package com.tea.utils;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

@Data
public class Result<T> {
    private int code;
    private String msg;
    private T data;

    // 无参构造函数
    public Result() {
    }

    // 有参构造函数
    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    // 静态方法快速创建成功结果
    public static <T> Result<T> success(T data) {
        return new Result<>(200, "success", data);
    }
    public static <T> Result<T> success() {
        return new Result<>(200, "success",null);
    }
    // 静态方法快速创建失败结果
    public static <T> Result<T> error(String msg) {
        return new Result<>(500, msg, null);
    }
    public static <T> Result<T> error(int code,String msg) {
        return new Result<>(code, msg, null);
    }

    // 判断是否成功
    @JsonIgnore
    public boolean isSuccess() {
        return this.code == 200;
    }

    // 获取消息
    @JsonIgnore
    public String getMessage() {
        return this.msg;
    }
}